const con = require("../utils/voteConnect")

// 拿到用户的投票
// 数据库中doing为1时, 说明此次是当前投票, 查找到当前投票
function voteFind() {
  const sql = `SELECT * FROM votes WHERE doing=1`
  return new Promise(res => {
    con.query(sql, (err, data) => {
      if (err) {
        console.log(err);
        return res({
          state: 0,
          message: "查询失败"
        })
      }
      else {
        res({
          state: 1,
          message: "查询成功",
          data
        })
      }
    })
  })
}

// 将数据库里的所有投票doing都改为0
function voteEnd() {
  const sql = `UPDATE votes SET doing=0`
  return new Promise(res => {
    con.query(sql, (err, data) => {
      if (err) {
        console.log(err);
        return res({
          state: 0,
          message: "结束投票失败"
        })
      }
      else {
        res({
          state: 1,
          message: "结束上一次投票成功",
          data
        })
      }
    })
  })
}

// 存储用户username和userMessage
async function voted(id, username, appVersion, userAgent) {
  const res = await voteFind()
  if (!res.state) {
    return res({
      state: 0,
      message: "加入失败"
    })
  }

  // 验证用户已投票
  console.log(res.data[0].voted)
  const obj = JSON.parse(res.data[0].voted)

  if (obj) {
    const only = obj.some(ele => {
      if (ele.appVersion === appVersion && ele.userAgent === userAgent && ele.username === username) return true
    })

    if (only) {
      return {
        state: 0,
        message: "您此轮已投票,请勿反复投票"
      }
    }
  }

  // 选项对应投票数
  let options = JSON.parse(res.data[0].options)
  // 对应选项+1
  options[id] = options[id] + 1

  // 投票者信息数组
  let voted = res.data[0].voted

  if (!voted) {
    // 如果投票者为null,说明还无人投票
    // 则传voted为一个空数组,方便之后操作
    voted = []
  } else {
    // 如果不为null,说明有数据,则json转为数组
    voted = JSON.parse(voted)
  }

  // 加入此次用户投票信息
  voted.push({
    id,
    username,
    appVersion,
    userAgent
  })

  // 转化为json,准备放在数据库中
  options = JSON.stringify(options)
  voted = JSON.stringify(voted)

  // sql语句
  const $sql = `UPDATE votes SET options='${options}', voted='${voted}' WHERE doing=1`

  return new Promise(resolve => {
    con.query($sql, (err, data) => {
      if (err) {
        console.log(err)
        return resolve({
          state: 0,
          message: "加入失败"
        })
      }
      else {
        resolve({
          state: 1,
          message: "加入成功"
        })
      }
    })
  })
}

// 增加投票
async function addVote(options) {
  const res = await voteEnd()
  options = JSON.stringify(options)
  const sql = `INSERT INTO votes (id,options,doing) VALUES (null,'${options}',1)`
  return new Promise(res => {
    con.query(sql, (err, data) => {
      if (err) {
        console.log(err);
        return res({
          state: 0,
          message: "加入失败"
        })
      }
      else {
        res({
          state: 1,
          message: "加入成功"
        })
      }
    })
  })
}

// 增加选项
async function addChoose(options) {
  // sql语句
  const $sql = `UPDATE votes SET options='${options}' WHERE doing=1`

  return new Promise(resolve => {
    con.query($sql, (err, data) => {
      if (err) {
        console.log(err)
        return resolve({
          state: 0,
          message: "增加选项失败"
        })
      }
      else {
        resolve({
          state: 1,
          message: "加入成功"
        })
      }
    })
  })
}

module.exports = { voted, addVote, voteFind, addChoose }